/*
 * Copyright 2011-2012 by The Regents of the University of California Licensed
 * under the Apache License, Version 2.0 (the "License"); you may not use this
 * file except in compliance with the License. you may obtain a copy of the
 * License from
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package edu.ucsd.db.bassarisk.data.api;

import java.io.Serializable;
import java.nio.ByteBuffer;
import java.util.Comparator;

public interface IValueType extends Serializable {

  public static interface IBufferedValueReference<PT extends IValueType> extends
      IValueReference<PT> {
    public ByteBuffer getBuffer();

    public int getLength();

    public int getStartOffset();

    public void reset(final ByteBuffer newBuffer, final int newOffset,
                      final int newLength);

    public void setBuffer(final ByteBuffer newBuffer);

    public void setLength(final int newLength);

    public void setStartOffset(final int newOffset);
  }

  public static interface IObjectValueReference<PT extends IValueType> extends
      IValueReference<PT> {}

  public static interface IReferenceSort {
    public static enum ReferenceSortOption {
      BYTE_BUFFER_SERIALIZED,
      JAVA_OBJECT,
    }

    public IValueType getReferenceType();

    public ReferenceSortOption getSortOption();

    public ITypeTools<?> getTypeTools();
  }

  public static interface ITypeTools<PT extends IValueType> {
    public IValueCodec<PT> getFastCodec(final IReferenceSort refType);

    public Comparator<IValueReference<PT>>
        getFastComparator(final IReferenceSort leftRefType,
                          final IReferenceSort rightRefType);

    public IValueCopier<PT> getFastCopier(final IReferenceSort leftRefType,
                                          final IReferenceSort rightRefType);

    public IValueCodec<PT> getGenericCodec();

    public Comparator<IValueReference<PT>> getGenericComparator();

    public IValueCopier<PT> getGenericCopyer();

    public IBufferedValueReference<PT> newBufferedReference();

    public IObjectValueReference<PT> newReference();
  }

  public static interface IValueCodec<PT extends IValueType> {
    public
        void
        setBufferedValueReference(final IValueReference<PT> valueSource,
                                  final IBufferedValueReference<PT> valueDestination);

    public void
        setConcreteValueReference(final IValueReference<PT> valueSource,
                                  final IObjectValueReference<PT> valueDestination);
  }

  public static interface IValueCopier<PT extends IValueType> {
    public void copy(final IValueReference<PT> sourceReference,
                     final IValueReference<PT> destinationReference);
  }

  public static interface IValueReference<PT extends IValueType> {
    public IValueType getType();

    public void resetDefault();

    public String toString(); // for debugging
  }

  long getUid();
}
